use std::fs::File;
use std::io::Read;
use std::path::Path;
use crate::extends::core::config::app_context::AppContext;


/// 初始化应用程序的启动横幅
///
/// # 参数
/// * `file_path` - 字符串形式的文件路径，指定包含横幅内容的文本文件路径
/// * `app` - 应用程序上下文信息，包含应用程序的配置和元数据
///
/// # 示例
/// ```rust
/// use crate::extends::core::config::app_context::AppContext;
/// init_banner("/path/to/banner.txt", &AppContext::default());
/// ```
pub fn init_banner(file_path: &str, app: &AppContext) {
    let path = Path::new(file_path);
    let display = path.display();

    // 尝试打开文件，如果失败则 panic 并输出错误信息
    let mut file = match File::open(&path) {
        Ok(file) => file,
        Err(why) => panic!("Failed to open {}: {:?}", display, why),
    };

    let mut s = String::new();
    // 尝试读取文件内容并格式化横幅信息，如果失败则 panic 并输出错误信息
    match file.read_to_string(&mut s) {
        Ok(_) => {
            s.push_str(&format!("==================================================== Welcome to {} ! ======================================================{}", &app.application.name, "\n"));
            s.push_str(&format!("{}{}{}", "Application: ", &app.application.name, "\n"));
            s.push_str(&format!("{}{}{}", "Version: ", &app.application.version, "\n"));
            s.push_str(&format!("{}{}{}", "Author: ", &app.application.author, "\n"));
            s.push_str(&format!("{}{}{}", "Email: ", &app.application.email, "\n"));
            s.push_str(&format!("{}{}{}", "DEPLOY_ENV: ", &app.application.profiles.active, "\n"));
            s.push_str("=================================================================== End ====================================================================\n");
            log::info!("\n{} contains:\n{}", display, s);
        }
        Err(e) => log::error!("Failed to read {}: {:?}", display, e),
    }
    // 文件在此处离开作用域，因此文件将被关闭。
}
